package com.canbot.u05.activity.shoppingGuide.codeMarketing;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.canbot.u05.R;
import com.canbot.u05.activity.camera.QRScanActivity;
import com.canbot.u05.bean.codeMarketing.CodeMarketingInfo;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.utils.qr.QRCodeTools;
import com.google.zxing.Result;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zmp
 * 二维码营销选择图片
 */
public class CodeModuleActivity extends BaseCodeFinishActivity {


        @BindView(R.id.code_iv_back)
        Button codeIvBack;

        @BindView(R.id.code_iv_next)
        Button codeIvNext;

        @BindView(R.id.module_title_et)
        EditText moduleTitleEt;

        @BindView(R.id.module_code_et)
        EditText moduleCodeEt;

        @BindView(R.id.module_update_iv)
        ImageView moduleUpdateIv;

        @BindView(R.id.module_update_iv2)
        ImageView moduleUpdateIv2;

        @BindView(R.id.module_code_et2)
        EditText moduleCodeEt2;

        @BindView(R.id.module_update_iv3)
        ImageView moduleUpdateIv3;

        @BindView(R.id.module_code_et3)
        EditText moduleCodeEt3;

        @BindView(R.id.code_ll_1)
        LinearLayout codeLl1;

        @BindView(R.id.code_ll_2)
        LinearLayout codeLl2;

        @BindView(R.id.code_ll_3)
        LinearLayout codeLl3;

        private ToastTools toastTools;

        private static final int Q_PIC_CODE = 1001;

        private static final int Q_SCAN_CODE = 1002;

        private AlertDialog alertDialog;

        private int currentUpIvID;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_code_module_one);
                ButterKnife.bind(this);
                toastTools = ToastTools.newInstance(this);
                initView();
                initData();
        }

        private void initView() {
                int type = CodeDataHelper.getInstance().getCodeMarketingInfo().getType();
                switch (type) {
                        case 1:
                                codeLl1.setVisibility(View.VISIBLE);
                                break;
                        case 2:
                                codeLl1.setVisibility(View.VISIBLE);
                                codeLl2.setVisibility(View.VISIBLE);
                                break;
                        case 3:
                                codeLl1.setVisibility(View.VISIBLE);
                                codeLl2.setVisibility(View.VISIBLE);
                                codeLl3.setVisibility(View.VISIBLE);
                                break;
                }
                moduleTitleEt.addTextChangedListener(titleWatcher);
                moduleCodeEt.addTextChangedListener(watcher);
                moduleCodeEt2.addTextChangedListener(watcher2);
                moduleCodeEt3.addTextChangedListener(watcher3);
        }

        private TextWatcher titleWatcher = new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (s.length() > 15) {
                                moduleTitleEt.setText(s.subSequence(0, 15));
                                moduleTitleEt.setSelection(15);
                                toastTools.showText(getString(R.string.input_up_words5));
                        }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
        };
        private TextWatcher watcher = new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (s.length() > 10) {
                                moduleCodeEt.setText(s.subSequence(0, 10));
                                moduleCodeEt.setSelection(10);
                                toastTools.showText(getString(R.string.input_up_words6));
                        }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
        };
        private TextWatcher watcher2 = new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (s.length() > 10) {
                                moduleCodeEt2.setText(s.subSequence(0, 10));
                                moduleCodeEt2.setSelection(10);
                                toastTools.showText(getString(R.string.input_up_words6));
                        }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
        };

        private TextWatcher watcher3 = new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (s.length() > 10) {
                                moduleCodeEt3.setText(s.subSequence(0, 10));
                                moduleCodeEt3.setSelection(10);
                                toastTools.showText(getString(R.string.input_up_words6));
                        }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
        };

        @OnClick({R.id.code_iv_back, R.id.code_iv_next, R.id.module_update_iv, R.id.module_update_iv2, R.id.module_update_iv3})
        public void onViewClicked(View view) {
                switch (view.getId()) {
                        case R.id.code_iv_back:
                                finish();
                                break;
                        case R.id.code_iv_next:
                                checkData();
                                break;
                        case R.id.module_update_iv:
                                currentUpIvID = R.id.module_update_iv;
                                showAlertDialog();
                                break;

                        case R.id.module_update_iv2:
                                currentUpIvID = R.id.module_update_iv2;
                                showAlertDialog();
                                break;
                        case R.id.module_update_iv3:
                                currentUpIvID = R.id.module_update_iv3;
                                showAlertDialog();
                                break;
                }
        }

        private void showAlertDialog() {
                if (alertDialog == null) {
                        alertDialog = new AlertDialog
                                .Builder(this, R.style.Theme_AppCompat_Light_Dialog_Alert)
                                .setTitle("上传二维码")
                                .setSingleChoiceItems(new String[]{"扫描识别", "识别图片"}, -1, new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                                dialogInterface.dismiss();
                                                switch (i) {
                                                        case 0:
                                                                startForResultActivity(QRScanActivity.class, Q_SCAN_CODE);
                                                                break;
                                                        case 1:
                                                                startForResultActivity(CodeChoosePicActivity.class, Q_PIC_CODE);
                                                                break;
                                                        default:
                                                                break;
                                                }
                                        }
                                }).create();
                }
                alertDialog.show();
        }

        private void checkData() {
                String title = moduleTitleEt.getText().toString().trim();
                String codeEt1 = moduleCodeEt.getText().toString().trim();
                String codeEt2 = moduleCodeEt2.getText().toString().trim();
                String codeEt3 = moduleCodeEt3.getText().toString().trim();
                CodeMarketingInfo codeMarketingInfo = CodeDataHelper.getInstance().getCodeMarketingInfo();
                String codeData1 = codeMarketingInfo.getCodeInfo1().getCodeData();
                String codeData2 = codeMarketingInfo.getCodeInfo2().getCodeData();
                String codeData3 = codeMarketingInfo.getCodeInfo3().getCodeData();
                int type = codeMarketingInfo.getType();
                switch (type) {
                        case 1:
                                if (TextUtils.isEmpty(title) || TextUtils.isEmpty(codeEt1)
                                    || TextUtils.isEmpty(codeData1)) {
                                        toastTools.showText(getString(R.string.content_completely));
                                        return;
                                }
                                codeMarketingInfo.setTitle(title);
                                codeMarketingInfo.getCodeInfo1().setCodeTitle(codeEt1);
                                break;
                        case 2:
                                if (TextUtils.isEmpty(title) || TextUtils.isEmpty(codeEt1) || TextUtils.isEmpty(codeEt2)
                                    || TextUtils.isEmpty(codeData1) || TextUtils.isEmpty(codeData2)) {
                                        toastTools.showText(getString(R.string.content_completely));
                                        return;
                                }
                                codeMarketingInfo.setTitle(title);
                                codeMarketingInfo.getCodeInfo1().setCodeTitle(codeEt1);
                                codeMarketingInfo.getCodeInfo2().setCodeTitle(codeEt2);
                                break;
                        case 3:
                                if (TextUtils.isEmpty(title) || TextUtils.isEmpty(codeEt1)
                                    || TextUtils.isEmpty(codeEt2) || TextUtils.isEmpty(codeEt3)
                                    || TextUtils.isEmpty(codeData1) || TextUtils.isEmpty(codeData2)
                                    || TextUtils.isEmpty(codeData3)) {
                                        toastTools.showText(getString(R.string.content_completely));
                                        return;
                                }
                                codeMarketingInfo.setTitle(title);
                                codeMarketingInfo.getCodeInfo1().setCodeTitle(codeEt1);
                                codeMarketingInfo.getCodeInfo2().setCodeTitle(codeEt2);
                                codeMarketingInfo.getCodeInfo3().setCodeTitle(codeEt3);
                                break;
                        default:
                                toastTools.showText(getString(R.string.type_error));
                                break;
                }
                MapVoiceAndEyes.CODE_MARKETING[(int) (Math.random() * 2)+4].playMapVoiceAndEyes();
                Intent intent = new Intent(this, CodeVoiceSetActivity.class);
                CodeMarketingInfo codeMarketingSave = CodeDataHelper.getInstance().getCodeMarketingSave();
                if (codeMarketingSave != null) {
                        intent.putExtra("path", codeMarketingSave.getVoiceContent());
                        intent.putExtra("content", codeMarketingSave.getVoiceContent());
                        intent.putExtra("type", codeMarketingSave.getVoiceType());
                }
                startOtherActivity(intent,false);
        }

        private void initData() {
                CodeMarketingInfo codeMarketingSave = CodeDataHelper.getInstance().getCodeMarketingSave();
                CodeMarketingInfo codeMarketingInfo = CodeDataHelper.getInstance().getCodeMarketingInfo();
                if (codeMarketingSave==null||codeMarketingSave.getType()!=codeMarketingInfo.getType()){
                        return;
                }
                moduleTitleEt.setText(codeMarketingSave.getTitle());
                int type = codeMarketingInfo.getType();
                switch (type) {
                        case 1:
                                setCode1(codeMarketingSave, codeMarketingInfo);
                                break;
                        case 2:
                                setCode1(codeMarketingSave, codeMarketingInfo);
                                setCode2(codeMarketingSave, codeMarketingInfo);
                                break;
                        case 3:
                                setCode1(codeMarketingSave, codeMarketingInfo);
                                setCode2(codeMarketingSave, codeMarketingInfo);
                                setCode3(codeMarketingSave, codeMarketingInfo);
                                break;
                }
        }

        private void setCode1(CodeMarketingInfo codeMarketingSave, CodeMarketingInfo codeMarketingInfo) {
                String codeTitle = codeMarketingSave.getCodeInfo1().getCodeTitle();
                String codeData = codeMarketingSave.getCodeInfo1().getCodeData();
                Bitmap resource = BitmapFactory.decodeResource(getResources(), R.drawable.uu_logo_code);
                Bitmap image = QRCodeTools.createImage(codeData, 400, 400, resource);
                moduleCodeEt.setText(codeTitle);
                moduleUpdateIv.setImageBitmap(image);
                codeMarketingInfo.getCodeInfo1().setCodeData(codeData);
        }
        private void setCode2(CodeMarketingInfo codeMarketingSave, CodeMarketingInfo codeMarketingInfo) {
                String codeTitle = codeMarketingSave.getCodeInfo2().getCodeTitle();
                String codeData = codeMarketingSave.getCodeInfo2().getCodeData();
                Bitmap resource = BitmapFactory.decodeResource(getResources(), R.drawable.uu_logo_code);
                Bitmap image = QRCodeTools.createImage(codeData, 400, 400, resource);
                moduleCodeEt2.setText(codeTitle);
                moduleUpdateIv2.setImageBitmap(image);
                codeMarketingInfo.getCodeInfo2().setCodeData(codeData);
        }
        private void setCode3(CodeMarketingInfo codeMarketingSave, CodeMarketingInfo codeMarketingInfo) {
                String codeTitle = codeMarketingSave.getCodeInfo3().getCodeTitle();
                String codeData = codeMarketingSave.getCodeInfo3().getCodeData();
                Bitmap resource = BitmapFactory.decodeResource(getResources(), R.drawable.uu_logo_code);
                Bitmap image = QRCodeTools.createImage(codeData, 400, 400, resource);
                moduleCodeEt3.setText(codeTitle);
                moduleUpdateIv3.setImageBitmap(image);
                codeMarketingInfo.getCodeInfo3().setCodeData(codeData);
        }


        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                final CodeMarketingInfo marketingInfo = CodeDataHelper.getInstance().getCodeMarketingInfo();
                if (requestCode == Q_PIC_CODE && resultCode == RESULT_OK) {
                        String picPath = data.getStringExtra("picPath");
                        QRCodeTools.analyzeBitmap(picPath, new QRCodeTools.AnalyzeCallback() {
                                @Override
                                public void onAnalyzeSuccess(Bitmap mBitmap, Result result, int sampleSize) {
                                        String text = result.getText();
                                        Bitmap resource = BitmapFactory.decodeResource(getResources(), R.drawable.uu_logo_code);
                                        Bitmap image = QRCodeTools.createImage(text, 400, 400, resource);
                                        switch (currentUpIvID) {
                                                case R.id.module_update_iv:
                                                        marketingInfo.getCodeInfo1().setCodeData(text);
                                                        moduleUpdateIv.setImageBitmap(image);
                                                        break;
                                                case R.id.module_update_iv2:
                                                        marketingInfo.getCodeInfo2().setCodeData(text);
                                                        moduleUpdateIv2.setImageBitmap(image);
                                                        break;
                                                case R.id.module_update_iv3:
                                                        marketingInfo.getCodeInfo3().setCodeData(text);
                                                        moduleUpdateIv3.setImageBitmap(image);
                                                        break;
                                                default:
                                        }
                                }

                                @Override
                                public void onAnalyzeFailed() {
                                        toastTools.showText(getString(R.string.invalid_image));
                                }
                        });
                }
                if (requestCode == Q_SCAN_CODE && resultCode == RESULT_OK) {
                        String codeResult = data.getStringExtra("codeResult");
                        Bitmap resource = BitmapFactory.decodeResource(getResources(), R.drawable.uu_logo_code);
                        Bitmap image = QRCodeTools.createImage(codeResult, 400, 400, resource);
                        switch (currentUpIvID) {
                                case R.id.module_update_iv:
                                        marketingInfo.getCodeInfo1().setCodeData(codeResult);
                                        moduleUpdateIv.setImageBitmap(image);
                                        break;
                                case R.id.module_update_iv2:
                                        marketingInfo.getCodeInfo2().setCodeData(codeResult);
                                        moduleUpdateIv2.setImageBitmap(image);
                                        break;
                                case R.id.module_update_iv3:
                                        marketingInfo.getCodeInfo3().setCodeData(codeResult);
                                        moduleUpdateIv3.setImageBitmap(image);
                                        break;
                                default:
                        }
                }
        }

}
